import os
from typing import Literal, Optional

from flask import Flask

from .configs import config
from .extensions import init_plugs
from .scripts import init_scripts
from .view import init_view


def create_app(config_name: Optional[Literal['development', 'testing', 'production']] = None):
    app = Flask(__name__)

    if not config_name:
        config_name = os.getenv('FLASK_ENV', 'production')
    app.config.from_object(config[config_name])

    upload_folder = app.config['UPLOAD_FOLDER']
    if not os.path.exists(upload_folder):
        os.mkdir(upload_folder)

    init_plugs(app)
    init_view(app)
    init_scripts(app)

    return app
